package com.wholesale.utils;

import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;

import com.wholesale.R;
import com.wholesale.beans.CollectGoods;
import com.wholesale.beans.GoodsInfo;
import com.wholesale.datas.WebServiceClient;
import com.wholesale.utils.AsyncImageLoader.ImageCallback;

public class FavoriteListAdapter extends ArrayAdapter<CollectGoods> {

	// ListView中的数据来源
	private ListView listView;
	private AsyncImageLoader loader;
	private boolean isDelete = false;
	private FavoriteCache viewCache;

	public FavoriteListAdapter(Context context, ListView listView) {
		super(context, 0);
		this.listView = listView;
		loader = AsyncImageLoader.getInstance();
	}

	public FavoriteListAdapter(Context context,
			List<CollectGoods> collectGoodsList, ListView listView) {
		super(context, 0, collectGoodsList);
		this.listView = listView;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		Activity activity = (Activity) getContext();

		// Inflate the views from XML
		View rowView = convertView;
		if (rowView == null) {
			LayoutInflater inflater = activity.getLayoutInflater();
			rowView = inflater.inflate(R.layout.favoriteitems, null);
			viewCache = new FavoriteCache(rowView);
			rowView.setTag(viewCache);
		} else {
			viewCache = (FavoriteCache) rowView.getTag();
		}
		CollectGoods collectGoods = getItem(position);
		GoodsInfo goodsInfo = collectGoods.getGoodsInfo();

		// Load the image and set it on the ImageView
		String imageUrl = goodsInfo.getGoodsThumb();
		ImageView imageView = viewCache.getGoodsImage();
		imageView.setTag(imageUrl);
		Drawable cachedImage = loader.loadDrawable(imageUrl,
				new ImageCallback() {
					public void imageLoaded(Drawable imageDrawable,
							String imageUrl) {
						ImageView imageViewByTag = (ImageView) listView
								.findViewWithTag(imageUrl);
						if (imageViewByTag != null) {
							imageViewByTag.setImageDrawable(imageDrawable);
						}
					}
				});
		if (cachedImage == null) {
			imageView.setImageResource(R.drawable.loading);
		} else {
			imageView.setImageDrawable(cachedImage);
		}
		// Set the text on the TextView
		if (isDelete) {
			viewCache.getDeteleButton().setVisibility(View.VISIBLE);
			viewCache.getDeteleButton().setOnClickListener(
					new lvButtonListener(position));
		} else {
			viewCache.getDeteleButton().setVisibility(View.GONE);
		}
		viewCache.getGoodsName().setText(goodsInfo.getGoodsName());
		viewCache.getGoodsPrice().setText("US$ " + goodsInfo.getShopPrice());
		viewCache.getGoodsPrice().setTextColor(Color.RED);
		// if (goodsInfo.isNew()) {
		// viewCache.getNewImage().setVisibility(View.VISIBLE);
		// } else {
		// viewCache.getNewImage().setVisibility(View.INVISIBLE);
		// }
		// if (goodsInfo.isPromote()) {
		// viewCache.getPromoteImage().setVisibility(View.VISIBLE);
		// } else {
		// viewCache.getPromoteImage().setVisibility(View.INVISIBLE);
		// }
		return rowView;
	}

	public void setDelete(boolean Delete) {
		isDelete = Delete;
	}

	/**
	 * 批量添加数据
	 * 
	 * @param items
	 */
	public void addItems(List<CollectGoods> items) {
		for (CollectGoods collectGoods : items) {
			this.add(collectGoods);
		}
	}

	class DeleteFavoriteListAsync extends
			AsyncTask<CollectGoods, Integer, Boolean> {
		ProgressDialog pdialog;
		int position ;
		public DeleteFavoriteListAsync(Context context,int position) {
			pdialog = new ProgressDialog(context);
			this.position = position ;
			pdialog.setTitle("Deleteing...");
			pdialog.setMessage("Please wait...");
			pdialog.setCancelable(true);
			pdialog.setButton("cancel", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int i) {
					dialog.cancel();
					DeleteFavoriteListAsync.this.cancel(true);
				}
			});
			pdialog.show();
		}

		@Override
		protected Boolean doInBackground(CollectGoods... params) {
			return WebServiceClient.operateCollectGoods(params[0], 2, Constants.loginResponse.getUserToken());
		}

		@Override
		protected void onPostExecute(Boolean result) {
			if(result){
				FavoriteListAdapter.this.remove(FavoriteListAdapter.this
						.getItem(position));
			}else{
				AlertDialog.Builder builder = new Builder(FavoriteListAdapter.this.getContext());
				builder.setTitle("Error");
				builder.setMessage("Delete fail ");
				builder.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				});
				builder.create().show();
			}
			pdialog.dismiss();
		}

		@Override
		protected void onCancelled() {
			super.onCancelled();
		}
	}

	class lvButtonListener implements OnClickListener {
		private int position;

		lvButtonListener(int pos) {
			position = pos;
		}

		public void onClick(View v) {
			int vid = v.getId();
			if (vid == viewCache.getDeteleButton().getId()) {
				DeleteFavoriteListAsync deleteFavoriteListAsync = new DeleteFavoriteListAsync(FavoriteListAdapter.this.getContext(), position);
				deleteFavoriteListAsync.execute(FavoriteListAdapter.this
						.getItem(position));
			}
		}
	}

}
